
package Sokoban;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.*;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/*import com.brackeen.javagamebook.graphics.*;
import com.brackeen.javagamebook.tilegame.sprites.*;*/

/**
    Klasa ResourceManager Ĺ‚Ä…duje i zarzÄ…dza kafelkami w postaci obiektĂłw Image
    oraz "goĹ›ci" obiekty Sprite wykorzystywane w grze. Duszki w grze sÄ…
    powielane na podatawie duszkĂłw "gosci".
*/
public class ResourceManager {

    private ArrayList tiles; //przechowuje referencje do obrazkĂłw
   
    /*private int currentMap;
    private GraphicsConfiguration gc;

    // duszki wykorzystywane do klonowania
     
    */
    private MovingImage player;
    
    private MovingImage wall;
    
    private MovingImage boulder;
    
    private MovingImage area;
    
    /*
    private Sprite musicSprite;
    private Sprite coinSprite;
    private Sprite goalSprite;
    private Sprite grubSprite;
    private Sprite flySprite;

    /**
        Tworzy nowy obiekt ResourceManager z podanym
        obiektem GraphicsConfiguration.
    */
    /*public ResourceManager(GraphicsConfiguration gc) {
        this.gc = gc;
        loadTileImages();
        //loadCreatureSprites();
        //loadPowerUpSprites();
    }*/
    public ResourceManager() {
        loadTileImages();
        loadMovingImages();
    }
    
    public void loadTileImages() {
        // szukanie kafelkĂłw A,B,C itd. uĹ‚atwia to dodawanie
        // nowych kafelkĂłw do katalogu images/
        tiles = new ArrayList();
        char ch = 'A';
        while (true) {
            String name = "tile_" + ch + ".png";
            File file = new File("images/" + name);
            if (!file.exists()) {
                break;
            }
            tiles.add(loadImage(file));
            ch++;
        }
    }
    
    public void loadMovingImages() {

        File file1 = new File("images/star1.png");
        if (file1.exists()) 
        	 
        	player = new MovingImage(loadImage(file1));
        
        else 
        	System.out.println("Plik images/star1.png nie istnieje");
        
        File file2 = new File("images/heart3.png");
        if (file2.exists()) 
        	 
        	boulder = new MovingImage(loadImage(file2));
        
        else 
        	System.out.println("Plik images/heart3.png nie istnieje");
        /*File file2 = new File("images/ball.jpg");
        if (!file2.exists()) 
        	images[1] = loadImage(file2);
        else
        	System.out.println("Plik images/ball.jpg nie istnieje");*/
        
    }      
    
    /**
        Pobranie rysunkĂłw z katalogu images/.
    */
    public Image loadImage(File file) {
    	
    	
    	Image img=null;
    	
    	try {
            img = ImageIO.read(file);
        } catch (IOException e) {
        	
        }
		return img;
    }

   
   
/*
 * Ĺ‚aduje mapÄ™ z pliku map.txt
 */
    public TileMap loadMap(String filename)
        throws IOException
    {
        ArrayList lines = new ArrayList();
        int width = 0;
        int height = 0;

        // odczytanie wszystkich wierszy z pliku do listy
        BufferedReader reader = new BufferedReader(
            new FileReader(filename));
        while (true) {
            String line = reader.readLine();
            // koniec wierszy do odczytywania
            if (line == null) {
                reader.close();
                break;
            }

            // dodawanie wszystkich wierszy poza komentarzami
            if (!line.startsWith("#")) {
                lines.add(line);
                width = Math.max(width, line.length());
            }
        }

       
        height = lines.size();
        TileMap newMap = new TileMap(width, height);
        for (int y=0; y<height; y++) {
            String line = (String)lines.get(y);
            for (int x=0; x<line.length(); x++) {
                char ch = line.charAt(x);

                // zprawdzenie, czy znak reprezentuje kafelek A, B, C itd.
                int tile = ch - 'A';
                if (tile >= 0 && tile < tiles.size()) {
                    newMap.setTile(x, y, (Image)tiles.get(tile));
                    if (ch== 'I') {
                    	
                    	wall = new MovingImage((Image)tiles.get(tile));
                    	wall.setX(x);
                    	wall.setY(y);
                    	(newMap.getWalls()).add(wall);
                    }
                    if (ch== 'K') {
                    	
                    	area = new MovingImage((Image)tiles.get(tile));
                    	area.setX(x);
                    	area.setY(y);
                    	(newMap.getAreas()).add(area);
                    }
                }
                else if (ch == 'b'){
                	
                	String name = "tile_A.png";
                    File file = new File("images/" + name);
                    if (!file.exists()) {
                        break;
                    }
                	newMap.setTile(x, y, loadImage(file));
                	
                    MovingImage localBoulder = new MovingImage(boulder);
                    localBoulder.setX(x);
                    localBoulder.setY(y);
                    newMap.setBoulder(boulder);
                	(newMap.getBoulders()).add(localBoulder);
                  
                }
                // sprawdzenie, czy znak reprezentuje gracza
                else if (ch == 'p') {
                	String name = "tile_A.png";
                    File file = new File("images/" + name);
                    if (!file.exists()) {
                        break;
                    }
                	newMap.setTile(x, y, loadImage(file));
                    player.setX(x);
                    player.setY(y);
                    newMap.setPlayer(player);
                }/*
              
                else if (ch == '!') {
                    addSprite(newMap, musicSprite, x, y);
                }
                else if (ch == '*') {
                    addSprite(newMap, goalSprite, x, y);
                }
                else if (ch == '1') {
                    addSprite(newMap, grubSprite, x, y);
                }
                else if (ch == '2') {
                    addSprite(newMap, flySprite, x, y);
                }*/
            }
        }

        // dodanie gracza do mapy
        

        return newMap;
    }

   /* private void addMovingImage(TileMap map,
        MovingImage object, int tileX, int tileY)
    {
        if (host != null) {
            
        	MovingImage image = ()
            // centrowanie duszka
            sprite.setX(
                TileMapRenderer.tilesToPixels(tileX) +
                (TileMapRenderer.tilesToPixels(1) -
                sprite.getWidth()) / 2);

            // wyrĂłwnywanie duszka z doĹ‚u
            sprite.setY(
                TileMapRenderer.tilesToPixels(tileY + 1) -
                sprite.getHeight());

            // dodawanie do mapy
            map.addMovingImage(image);
        }
    }*/

    // -----------------------------------------------------------
    // kod Ĺ‚adowania duszkĂłw i rysunkĂłw
    // -----------------------------------------------------------


    

    
       

       
/*
        // tworzenie animacji stworĂłw
        Animation[] playerAnim = new Animation[4];
        Animation[] flyAnim = new Animation[4];
        Animation[] grubAnim = new Animation[4];
        for (int i=0; i<4; i++) {
            playerAnim[i] = createPlayerAnim(
                images[i][0], images[i][1], images[i][2]);
            flyAnim[i] = createFlyAnim(
                images[i][3], images[i][4], images[i][5]);
            grubAnim[i] = createGrubAnim(
                images[i][6], images[i][7]);
        }

        // tworzenie duszkĂłw stworĂłw
        playerSprite = new Player(playerAnim[0], playerAnim[1],
            playerAnim[2], playerAnim[3]);
        flySprite = new Fly(flyAnim[0], flyAnim[1],
            flyAnim[2], flyAnim[3]);
        grubSprite = new Grub(grubAnim[0], grubAnim[1],
            grubAnim[2], grubAnim[3]);
    }

    private Animation createPlayerAnim(Image player1,
        Image player2, Image player3)
    {
        Animation anim = new Animation();
        anim.addFrame(player1, 250);
        anim.addFrame(player2, 150);
        anim.addFrame(player1, 150);
        anim.addFrame(player2, 150);
        anim.addFrame(player3, 200);
        anim.addFrame(player2, 150);
        return anim;
    }

    private Animation createFlyAnim(Image img1, Image img2,
        Image img3)
    {
        Animation anim = new Animation();
        anim.addFrame(img1, 50);
        anim.addFrame(img2, 50);
        anim.addFrame(img3, 50);
        anim.addFrame(img2, 50);
        return anim;
    }

    private Animation createGrubAnim(Image img1, Image img2) {
        Animation anim = new Animation();
        anim.addFrame(img1, 250);
        anim.addFrame(img2, 250);
        return anim;
    }

    private void loadPowerUpSprites() {
        // tworzenie duszka "serce"
        Animation anim = new Animation();
        anim.addFrame(loadImage("heart1.png"), 150);
        anim.addFrame(loadImage("heart2.png"), 150);
        anim.addFrame(loadImage("heart3.png"), 150);
        anim.addFrame(loadImage("heart2.png"), 150);
        goalSprite = new PowerUp.Goal(anim);

        // tworzenie duszka "gwiazda"
        anim = new Animation();
        anim.addFrame(loadImage("star1.png"), 100);
        anim.addFrame(loadImage("star2.png"), 100);
        anim.addFrame(loadImage("star3.png"), 100);
        anim.addFrame(loadImage("star4.png"), 100);
        coinSprite = new PowerUp.Star(anim);

        // tworzenie duszka "muzyka"
        anim = new Animation();
        anim.addFrame(loadImage("music1.png"), 150);
        anim.addFrame(loadImage("music2.png"), 150);
        anim.addFrame(loadImage("music3.png"), 150);
        anim.addFrame(loadImage("music2.png"), 150);
        musicSprite = new PowerUp.Music(anim);
    }*/
    
        
    }



